/*   **********************************************************************  **
 **   Copyright notice                                                       **
 **                                                                          **
 **   (c) 2009, Around(J2)ME				                                 **
 **   All rights reserved.                                                   **
 **                                                                          **
 **   This program and the accompanying materials are made available under   **
 **   the terms of the New BSD License which accompanies this    			 **
 **   distribution.											                 **
 **   A copy is found in the textfile LICENSE.txt							 **
 **                                                                          **
 **   This copyright notice MUST APPEAR in all copies of the file!           **
 **                                                                          **
 **   Main developers:                                                       **
 **     Juri Strumpflohner		http://blog.js-development.com	             **
 **		Matthias Braunhofer		http://matthias.jimdo.com                    **
 **                                                                          **
 **  **********************************************************************  */

package com.aroundj2me.model;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.jdom.JDOMException;

import com.aroundj2me.utility.Constants;

public class XMLManagerTest extends TestCase {
	private String continent;
	private XMLManager xmlManager;

	protected void setUp() throws Exception {
		super.setUp();
		continent = "Europe";
		xmlManager = new XMLManager(continent);
	}

	protected void tearDown() throws Exception {
		super.tearDown();
		continent = null;
		xmlManager = null;
	}

	public void testEncodePlacesToXML() {
		List<Place> places = getDummyPlaces();
		String xmlString = xmlManager.encodePlacesToXML(places);
		
		assertNotNull(xmlString);
		assertTrue("Type should be " + Constants.DATA_PLACES, xmlString.contains("<type>" + Constants.DATA_PLACES + "</type>"));
		
		for (Place place : places) {
			assertTrue("Name of place", xmlString.contains("<name>" + place.getName() + "</name>"));
			assertTrue("Address of place", xmlString.contains("<address>" + place.getAddress() + "</address>"));
			assertTrue("PostalCode of place", xmlString.contains("<postalCode>" + place.getPostalCode() + "</postalCode>"));
			assertTrue("City of place", xmlString.contains("<city>" + place.getCity() + "</city>"));
			assertTrue("Country of place", xmlString.contains("<country>" + place.getCountry() + "</country>"));
			assertTrue("Continent of place", xmlString.contains("<continent>" + place.getContinent() + "</continent>"));
			assertTrue("Phone of place", xmlString.contains("<phone>" + place.getPhone() + "</phone>"));
			assertTrue("Url of place", xmlString.contains("<url>" + place.getUrl() + "</url>"));
			for (String category : place.getCategories()) {
				assertTrue("Category of place", xmlString.contains("<category>" + category + "</category>"));
			}
			assertTrue("Latitude of place", xmlString.contains("<lat>" + place.getLat() + "</lat>"));
			assertTrue("Longitude of place", xmlString.contains("<lon>" + place.getLon() + "</lon>"));
		}
	}

	public void testEncodeAlert() {
		String alertType = "error";
		String alertMessage = "An error has occured! The requests couldn't be fulfilled!";
		
		String xmlResponseStr = xmlManager.encodeAlert(alertType, alertMessage);
		
		assertNotNull(xmlResponseStr);
		
		assertTrue("alertType is correct", xmlResponseStr.contains("<level>" + alertType + "</level>"));
		assertTrue("size is correct",  xmlResponseStr.contains("<size>" + 0 + "</size>"));
		assertTrue("messages is correct", xmlResponseStr.contains("<message>" + alertMessage + "</message>"));
	
	}
	
	public void testReadPlaces() throws JDOMException, IOException {
		List<Place> resultPlaces = xmlManager.readPlaces(new ByteArrayInputStream(createPlacesXML().getBytes()));
		assertNotNull(resultPlaces);
		
		assertEquals("List should contain 3 places", 3, resultPlaces.size());
		
		for (int i = 0; i < 3; i++) {
			assertEquals("Equal names", "Place name n. " + i, resultPlaces.get(i).getName());
			assertEquals("Equal addresses", "Place address n. " + i, resultPlaces.get(i).getAddress());
			assertEquals("Equal postalCodes", "Place postalCode n. " + i, resultPlaces.get(i).getPostalCode());
			assertEquals("Equal cities", "Place city n. " + i, resultPlaces.get(i).getCity());
			assertEquals("Equal countries", "Place country n. " + i, resultPlaces.get(i).getCountry());
			assertEquals("Equal continents", continent, resultPlaces.get(i).getContinent());
			assertEquals("Equal phones", "Place phone n. " + i, resultPlaces.get(i).getPhone());
			assertEquals("Equal urls", "Place url n. " + i, resultPlaces.get(i).getUrl());
			for (String category : resultPlaces.get(i).getCategories()) {
				assertEquals("Equal categories", "Place category n. " + i, category);
			}		
			assertEquals("Equal latitudes", i, (int)resultPlaces.get(i).getLat());
			assertEquals("Equal longitudes", i, (int)resultPlaces.get(i).getLon());
		}
	}
	
	private List<Place> getDummyPlaces() {
		List<Place> dummyPlaces = new ArrayList<Place>();
		for(int i = 0; i < 3; i++){
			Place p = new Place();
			p.setName("Place name n. " + i);
			p.setAddress("Place address n. " + i);
			p.setPostalCode("Place postalCode n. " +i);
			p.setCity("Place city n. " + i);
			p.setCountry("Place country n. " +i);
			p.setContinent(continent);
			p.setPhone("Place phone n. " + i);
			p.setUrl("Place url n. " +i);
			List<String> categories = new ArrayList<String>();
			categories.add("Place category n. " + i);
			p.setCategories(categories);
			p.setLat(i);
			p.setLon(i);
			dummyPlaces.add(p);
		}
		return dummyPlaces;
	}
	
	private String createPlacesXML() {
		String result = "<places>";
		for (int i = 0; i < 3; i++) {
			result += "<place>" +
					  "	<name>Place name n. " + i + "</name>"+
					  "	<address>Place address n. " + i + "</address>" + 
					  "	<postalCode>Place postalCode n. " + i + "</postalCode>" +
					  "	<city>Place city n. " + i + "</city>" +
					  "	<country>Place country n. " + i + "</country>" +
					  "	<continent>" + continent + "</continent>" +
					  "	<phone>Place phone n. " + i + "</phone>" +
					  "	<url>Place url n. " + i + "</url>" +
					  "	<categories>" +
					  "		<category>Place category n. " + i + "</category>" +
					  "	</categories>" + 
					  "	<location>" +
					  "		<lat>" + i + "</lat>" + 
					  "		<lon>" + i + "</lon>" + 
					  "	</location>" + 
					  "</place>";
		}
		result += "</places>";
		return result;		  
	}
}
